<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ExperienceAdminController
 *
 * @author dro
 */

namespace Gairal\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Gairal\CoreBundle\Form\ExperienceType;
use Gairal\CoreBundle\Entity\Experience;

class ExperienceAdminController extends Controller {
    
    public function indexAction() {
        
        $listExperience = $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Experience')
                ->findBy(array(), array('displayOrder' => 'asc'));
        $maxOrder = count($listExperience) > 0 ? $listExperience[count($listExperience) - 1]->getDisplayOrder() : 0;

        return $this->render('GairalAdminBundle:Experience:index.html.twig', array(
                    'action' => 'Admin - Experience',
                    'listExperience' => $listExperience,
                    'maxOrder' => $maxOrder
        ));
    }
    
    public function addAction() {
        
        $experience = new Experience();
        $form = $this->createForm(new ExperienceType(), $experience);
        $options = $form->get('image')->getConfig()->getOptions();
        $choices = $options['choice_list']->getChoices();

        // On récupère la requête
        $request = $this->get('request');
        // On vérifie qu'elle est de type POST
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {
                $maxOrder = $this->getDoctrine()
                        ->getManager()
                        ->getRepository('GairalCoreBundle:Category')
                        ->findMaxOrder();
                $experience->setDisplayOrder(++$maxOrder);

                $em = $this->getDoctrine()->getManager();
                $em->persist($experience);
                $em->flush();
                
                $this->get('session')->getFlashBag()->add('info', 'Experience '. $experience->getName() .' added');
                return $this->redirect($this->generateUrl('AdminExperience'));
            }
            else
                $this->get('session')->getFlashBag()->add('error', $form->getErrorsAsString());
        }

        return $this->render('GairalAdminBundle:Experience:add.html.twig', array(
                    'action' => 'Admin - Experience - Add',
                    'form' => $form->createView(),
                    'image_choices' => $choices
        ));
    }
    
    public function removeAction(Experience $experience) {
        if ($experience != null) {
            $this->getDoctrine()
                    ->getManager()
                    ->getRepository('GairalCoreBundle:Experience')
                    ->removeAndReorder($experience);
            $this->get('session')->getFlashBag()->add('info', 'Experience "' . $experience->getPlace() . '" removed');
        }
        else
            $this->get('session')->getFlashBag()->add('error', 'Experience not removed, error in url');

        return $this->redirect($this->generateUrl('AdminExperience'));
    }
    
    public function modifyAction(Experience $experience) {
        $form = $this->createForm(new ExperienceType(), $experience);
        $options = $form->get('image')->getConfig()->getOptions();
        $choices = $options['choice_list']->getChoices();

        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            if ($form->isValid()) {

                $em = $this->getDoctrine()->getManager();
                $em->persist($experience);
                $em->flush();

                $this->get('session')->getFlashBag()->add('info', 'Experience "' . $experience->getPlace() . '" modified');
                return $this->redirect($this->generateUrl('AdminExperience'));
            }
            else
                $this->get('session')->getFlashBag()->add('error', $form->getErrorsAsString());
        }
        return $this->render('GairalAdminBundle:Experience:modify.html.twig', array(
                    'action' => 'Admin - Experience - modify - '. $experience->getName(),
                    'form' => $form->createView(),
                    'experience' => $experience,
                    'image_choices' => $choices
        ));
    }
    
    

    public function upAction(Experience $experience) {
        $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Experience')
                ->swapWith($experience, $experience->getDisplayOrder() + 1);
        $this->get('session')->getFlashBag()->add('info', 'Experience "' . $experience->getName() . '" down\'d');

        return $this->redirect($this->generateUrl('AdminExperience'));
    }

    public function downAction(Experience $experience) {
        $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Experience')
                ->swapWith($experience, $experience->getDisplayOrder() - 1);
        $this->get('session')->getFlashBag()->add('info', 'Experience "' . $experience->getName() . '" up\'d');

        return $this->redirect($this->generateUrl('AdminExperience'));
    }
}

?>
